﻿using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Timers;

using System.IO.Ports;

using System.Diagnostics;

namespace ComTempV2
{

    /// <summary>

    /// Interaktionslogik für MainWindow.xaml

    /// </summary>

    /// 





    public partial class MainWindow : Window
    {

        private string strData = null;

        private int intData0 = 0;

        private int intData = 0;

        private int R = 0;

        private double T = 0;

        private double dblTemp = 0;

        private decimal decTemp = 0;

        private int intCount = 0;

        private int intValue = 0;

        private Stopwatch SW = new Stopwatch();

        private SerialPort port = null;





        public MainWindow()
        {

            InitializeComponent();



        }



        private void Button_Click_1(object sender, RoutedEventArgs e)
        {



            try
            {

                //   COMAsk();                                           --> wurde von mit gegen darunter folgende Zeile ersetzt >> haim



                comboBox.SelectedValue = true;

                port.BaudRate = 1200;

                port.DataBits = 8;

                port.Parity = Parity.None;

                port.StopBits = StopBits.One;

                port.Handshake = Handshake.None;

                port.DtrEnable = false;

                port.RtsEnable = false;

                port.ReadTimeout = 1000;

                port.Open();

                connection.Content = "Connected";

                connection.Foreground = new SolidColorBrush(Colors.Green);

                Timer1_Tick();

            }

            catch (Exception ex)
            {

                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");

                connection.Content = "Failed";

                connection.Foreground = new SolidColorBrush(Colors.Orange);

            }

        }



        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

            this.Close();

        }



        private void Button_Click_3(object sender, RoutedEventArgs e)
        {

            progessBar1.Value = 0;

            TextBlock1.Text = "Die Temperatur beträgt -°C";

            TempLabel.Content = "-°C";

            port.Close();

            connection.Content = "Disconnected";

            connection.Foreground = new SolidColorBrush(Colors.Red);

        }



        //  private void COMAsk() {

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {   // so klappt es besser >>haim



            for (int i = 0; i <= 4; i++)
            {

                if (comboBox.SelectedIndex.Equals(0))
                {

                    port = new SerialPort("COM1");



                }

                else if (comboBox.SelectedIndex.Equals(1))
                {

                    port = new SerialPort("COM2");



                }

                else if (comboBox.SelectedIndex.Equals(2))
                {

                    port = new SerialPort("COM3");



                }

                else if (comboBox.SelectedIndex.Equals(3))
                {

                    port = new SerialPort("COM4");



                }

            }

        }







        private void Timer1_Tick()
        {

            if (port.IsOpen) { port.Close(); }





            this.port.DtrEnable = true;

            this.port.RtsEnable = false;

            this.port.Open();

            connection.Content = Convert.ToString(this.port.IsOpen);

            SW.Reset();

            this.SerialPinChange();

            SW.Start();



            while (SW.IsRunning)
            {

                Thread.Sleep(0);



            }



        }





        //berechnung

        public void SerialPinChange()
        {

            SerialPinChangedEventArgs e = null;



            this.intData0 = System.Convert.ToInt32(SW.ElapsedMilliseconds);



            if (e.EventType == System.IO.Ports.SerialPinChange.DsrChanged)
            {

                SW.Stop();

            }

            else { }



            intData = System.Convert.ToInt32(intData0 * 1); // Faktor zur Anpassung der gemessenen Ladedauer

            intdatatext.Text = Convert.ToString(intData);





            this.R = System.Convert.ToInt32(2200 + 7800 * (intData - 76.3) / (294.6 - 76.3)); // Umrechnung von der Ladedauer zumakt. Widerstandswert des NTC

            /* R = CInt(2200 + 7800 * (intData - 1698) / (1921 - 1698))*/



            this.R = System.Math.Abs(R);              // Entfernung des Vorzeichens



            T = 1 / ((Math.Log(R / 10000) / 3977) + 1 / 298) - 273.15;              // Formel ist richtig!



            dblTemp = Math.Round(T, 2);                     // Rundung der Temperatur auf zweiNachkommastellen



            decTemp = Convert.ToDecimal(dblTemp);            // Konvertierung des Datenformatesvon DOUBLE zu DECIMAL



            port.DtrEnable = false;

            port.Close();

            this.DoUpdate();





        }



        private void DoUpdate()
        {

            // = Str(R) + " Ohm";

            // Label5.Text = Str$(intData) + " ms";

            TempLabel.Content = decTemp.ToString();

            progessBar1.Value = System.Convert.ToInt32(decTemp);

        }





    }



}